//============================================================================================
/**
 * @file	pokethlon_call.c
 * @brief	ポケスロン呼び出し
 * @author	Nozomu Saito
 *
 * モジュール名：PokethlonCall_
 */
//============================================================================================
#include "common.h"
#include "fieldsys.h"
#include "field/field.h"

#include "system/wipe.h"
#include "system/brightness.h"

#include "savedata/pkthln_svdata.h"

//ポケスロン
#include "application/pokethlon_frame.h"

#include "pokethlon_call.h"

static BOOL PokethlonEvent(GMEVENT_CONTROL * event);

typedef struct{
	int	Seq;
	POKETHLON_HEADER *Header;
	int IsConect;
	u16 CrsID;			//コースID
	u16 AllStar;

	u16 *Exit;			//ポケスロン中止フラグ格納バッファ
	u16 *Score;			//スコア格納バッファ
	u16 *ThlnPoint;		//買い物ポイント格納バッファ
	u16 *Rank;			//順位格納バッファ
}PKTHLN_CALL_EVENT_WORK;

typedef enum
{
	PKTHLN_CALL_START,
	PKTHLN_CALL_PROC,
	PKTHLN_CALL_PROC_WAIT,
	PKTHLN_CALL_END,
}PKTHLN_CALL_SEQ;

#ifdef PM_DEBUG
extern DEBUG_PKTHLN_WORK DebugPkthlnWk;
#endif

//==============================================================================
/**
 * ポケスロンイベント作成
 *
 * @param	fsys		フィールドシステムポインタ
 * @param	inIsConect	通信かどうか	0：非通信	1：通信
 * @param	inAllStar		オールスターフラグ
 * @param	inCrsID			コースＩＤ
 * @param	outExit			やめる終了か？
 * @param	outScore		スコア
 * @param	outThlnPoint	買い物ポイント
 * @param	outRank			順位
 *
 * @retval  none
 */
//==============================================================================
void PokethlonCall_SetupEvent(FIELDSYS_WORK * fsys, const u8 inIsConect, const u8 inAllStar, const u8 inCrsID,
		u16 *outExit, u16 *outScore, u16 *outThlnPoint, u16 *outRank)
{
	// イベントワーク作成
	PKTHLN_CALL_EVENT_WORK *wk = sys_AllocMemoryLo(HEAPID_WORLD, sizeof(PKTHLN_CALL_EVENT_WORK));
	wk->Seq = 0;
	wk->IsConect = inIsConect;
	wk->AllStar = inAllStar;
	wk->CrsID = inCrsID;
	wk->Exit = outExit;
	wk->Score = outScore;
	wk->ThlnPoint = outThlnPoint;
	wk->Rank = outRank;

	FieldEvent_Call(fsys->event, PokethlonEvent, wk);
}

//==============================================================================
/**
 * イベント
 *
 * @param	event	イベントポインタ
 *
 * @retval  BOOL	TRUE:イベント終了　FALSE：イベント継続
 */
//==============================================================================
static BOOL PokethlonEvent(GMEVENT_CONTROL * event)
{
	FIELDSYS_WORK * fsys = FieldEvent_GetFieldSysWork(event);
	PKTHLN_CALL_EVENT_WORK *wk  = FieldEvent_GetSpecialWork(event);

	switch(wk->Seq){
	case PKTHLN_CALL_START:
		{
			//ヘッダー情報作成
			wk->Header = sys_AllocMemoryLo(HEAPID_WORLD,sizeof(POKETHLON_HEADER));
			if (wk->IsConect){
				wk->Header->GameType = PKTHLN_GAME_COMM;
			}else{
				wk->Header->GameType = PKTHLN_GAME_ALONE;
			}
			wk->Header->SaveData = GameSystem_GetSaveData(fsys);
			wk->Header->CourseID = wk->CrsID;
			wk->Header->AllStar = wk->AllStar;
#ifdef PM_DEBUG
			if (DebugPkthlnWk.Flg){
				wk->Header->CourseID = DebugPkthlnWk.Crs;
			}
#endif

			Pokethlon_SetProc(fsys, wk->Header);
			FieldSystemProc_SeqHoldEnd();
		}
		wk->Seq = PKTHLN_CALL_PROC;
		break;
	case PKTHLN_CALL_PROC:
		//ポケスロンプロセス終了待ち
		if(FieldEvent_Cmd_WaitSubProcEnd(fsys)){
			break;
		}
		wk->Seq = PKTHLN_CALL_PROC_WAIT;
		//フィールドマッププロセス復帰
		FieldEvent_Cmd_SetMapProc(fsys);
		break;
	case PKTHLN_CALL_PROC_WAIT:
		//フィールドプロセス開始終了待ち
		if(FieldEvent_Cmd_WaitMapProcStart(fsys)){
			wk->Seq = PKTHLN_CALL_END;
			break;
		}
		break;
	case PKTHLN_CALL_END:
		//リターン値セット
		*wk->Exit = wk->Header->RetExit;
		*wk->Score = wk->Header->RetScore;
		*wk->ThlnPoint = wk->Header->RetThlonPt;
		*wk->Rank = wk->Header->RetRank;
		// イベントワーク解放
		sys_FreeMemoryEz(wk->Header);
		sys_FreeMemoryEz(wk);
		return TRUE;
	}

	return FALSE;
}